package com.daiyang.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.daiyang.blog.common.exception.BlogException;
import com.daiyang.blog.common.utils.JWTUtils;
import com.daiyang.blog.mapper.BlogInfoMapper;
import com.daiyang.blog.mapper.UserInfoMapper;
import com.daiyang.blog.pojo.BlogInfo;
import com.daiyang.blog.pojo.UserInfo;
import com.daiyang.blog.service.DTO.UserInfoResponse;
import com.daiyang.blog.service.DTO.UserLoginResponse;
import com.daiyang.blog.service.DTO.UserInfoParam;
import com.daiyang.blog.service.UserService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotBlank;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service("userServiceImpl")
public class UserServiceImpl implements UserService {
    @Resource(name = "userInfoMapper")
    private UserInfoMapper userInfoMapper;

    @Resource(name = "blogInfoMapper")
    private BlogInfoMapper blogInfoMapper;
    public UserLoginResponse login(UserInfoParam user) {
        //校验用户是否存在
        UserInfo userInfo = selectUserInfoByUserName(user.getUserName());
        if (userInfo == null|| userInfo.getId() == null) {
            throw new BlogException("⽤⼾不存在");
        }
        if(!userInfo.getPassword().equals(user.getPassword())){
            throw new BlogException("密码错误");
        }
        //账户密码正确
        Map<String, Object> map = new HashMap<>();
        map.put("id",userInfo.getId());
        map.put("name",user.getUserName());
        String jwt= JWTUtils.genToken(map);
        return new UserLoginResponse(userInfo.getId(), jwt);

    }

    @Override
    public UserInfoResponse getUserInfo(Integer userId) {
        UserInfoResponse userInfoResponse = new UserInfoResponse();
        UserInfo userInfo = selectUserInfoById(userId);
        BeanUtils.copyProperties(userInfo, userInfoResponse);
        return userInfoResponse;
    }



    @Override
    public UserInfoResponse selectAuthorInfoByBlogId(Integer blogId) {
        UserInfoResponse userInfoResponse = new UserInfoResponse();
        //1. 根据博客ID, 获取作者ID
        BlogInfo blogInfo = getBlogInfo(blogId);
        if(blogInfo == null){
            throw new BlogException("博客不存在");
        }
        //2. 根据作者ID, 获取作者信息
        UserInfo userInfo = selectUserInfoById(blogInfo.getUserId());

        //3. 复制作者信息到UserInfoResponse
        BeanUtils.copyProperties(userInfo, userInfoResponse);
        return userInfoResponse;
    }

    private BlogInfo getBlogInfo(Integer blogId) {
        return blogInfoMapper.selectOne(
                new LambdaQueryWrapper<BlogInfo>().eq(BlogInfo::getDeleteFlag, 0)
                       .eq(BlogInfo::getId, blogId));
    }

    private UserInfo selectUserInfoById(Integer userId) {
        return userInfoMapper.selectOne(
                new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getDeleteFlag, 0)
                        .eq(UserInfo::getId, userId));
    }

    private UserInfo selectUserInfoByUserName(@NotBlank(message = "⽤⼾名不能为空") @Length(max = 20, message = "⽤⼾名⻓度不能超过20") String userName) {
        return userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getUserName, userName)
                .eq(UserInfo::getDeleteFlag,0));
    }



}
